#include "engine.h"
#include "monkeybusiness.h"
#include "render/rendersystem.h"
#include "math/random.h"

/*
DEFINE_ENTITY(monkeybusiness_t,entity_t)
PROPERTY_BINDING_BEGIN(monkeybusiness_t)
	BIND_PROPERTY(monkeybusiness_t,monkeynum_horizontal,int);
	BIND_PROPERTY(monkeybusiness_t,monkeynum_vertical,int);
	BIND_PROPERTY(monkeybusiness_t,monkeynum_depth,int);
	BIND_PROPERTY(monkeybusiness_t,monkeynum_max,int);
	BIND_PROPERTY(monkeybusiness_t,monkeydist_horizontal,float);
	BIND_PROPERTY(monkeybusiness_t,monkeydist_vertical,float);
	BIND_PROPERTY(monkeybusiness_t,monkeydist_depth,float);
PROPERTY_BINDING_END


monkeybusiness_t::monkeybusiness_t()
{
	monkeynum_horizontal=3;
	monkeynum_vertical=3;
	monkeynum_depth=5;

	monkeynum_max=1000;

	monkeydist_horizontal=15;
	monkeydist_vertical=15;
	monkeydist_depth=30;

}

void monkeybusiness_t::init()
{
	entity_t::init();
	monkeys=new renderhierarchy_t[monkeynum_max];

	rendersystem_t::get_3d_object(monkeyproto,"objects/monkey/monkey");
//	monkeyproto->local_matrix.identity();

	monkeynum=0;
	int numx=monkeynum_horizontal;
	int numy=monkeynum_vertical;
	int numz=monkeynum_depth;
	float distx=monkeydist_horizontal;
	float disty=monkeydist_vertical;
	float distz=monkeydist_depth;

	for (int x=-numx; x<numx;++x)
		for (int y=-numy; y<numy;++y)
			for (int z=0; z<numz; ++z)
	{
		int n=monkeynum;
		++monkeynum;

		monkeys[n].children.push_back(monkeyproto);
		monkeys[n].local_matrix.identity();
		monkeys[n].local_matrix.from_rotation(sfrand(-10,10),0,0);
//		monkeys[n].m_local_matrix.scale(sfrand(minscale,maxscale));

		float3 pos;
		pos.x=(float)(distx*x);
		pos.y=(float)(disty*y);
		pos.z=(float)(distz*z);
		monkeys[n].local_matrix.axis(3)=pos;

		if (monkeynum==monkeynum_max)
		{
			goto _kiszall;
		}
	}

_kiszall:
;
}

void monkeybusiness_t::exit()
{
	for (int n=0; n<monkeynum; ++n)
	{
		monkeys[n].children.clear();
	}

	if (monkeys)
		delete [] monkeys;

	delete monkeyproto;
	entity_t::exit();
}

void monkeybusiness_t::update()
{
	entity_t::update();
	for (int n=0; n<monkeynum; ++n)
	{
		monkeys[n].node_color.w+=0.01f;
		if (monkeys[n].node_color.w>1)
			monkeys[n].node_color.w=1;
		monkeys[n].local_matrix.axis(3).z-=0.1f;
		float& z=monkeys[n].local_matrix.axis(3).z;
		float& a=monkeys[n].node_color.w;
		if (z<0)
		{
			z+=monkeynum_depth*monkeydist_depth;
			a=0;
		}
		if (z<monkeydist_depth)
		{
			a=z/monkeydist_depth;
		}
		else if (z>(monkeynum_depth-1)*monkeydist_depth)
		{
			a=1-(z-(monkeynum_depth-1)*monkeydist_depth)/monkeydist_depth;
		}
		else
			a=1;
		RSYS->render_object(&monkeys[n]);
	}
}
*/